package org.ignoramus.application.view.rcp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.chaorder.jface.viewers.ArrayCellLabelProvider;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.ignoramus.application.model.Cards;
import org.ignoramus.application.model.ReviewCard;
import org.ignoramus.application.view.CardView;
import org.ignoramus.application.view.MemoryMap;

public class SwtMemoryMap extends MemoryMap
{
//	final Table table;

	private final Cards cards;
	private final GridTableViewer v;
	private static final int default_cols = 20;
	private static final int default_rows = 15;
	private final double ratioWidthOnHeight = (double)default_cols/(double)default_rows;
	
	/**
	 * @param parent 
	 * @param cards
	 */
	public SwtMemoryMap(Composite parent, Cards cards)
	{
		this.cards = cards;
		int maxChars = 1;
		
		for (Iterator<ReviewCard> iterator = cards.values().iterator(); iterator.hasNext();)
		{
			ReviewCard card = iterator.next();
			String title = card.getTitle();
	        if(title.length() > maxChars)
	        {
	        	maxChars = title.length();
	        }
		}
		int cols = (int) Math.ceil(Math.sqrt(ratioWidthOnHeight * ((double)1 / (double)maxChars) * cards.size()));
		int rows = (int) Math.ceil((double)cards.size()/(double)cols);
		
		
		v = new GridTableViewer(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		v.setLabelProvider(new ArrayCellLabelProvider());
		v.setContentProvider(new ArrayContentProvider());
		v.getGrid().setCellSelectionEnabled(true);

		v.getGrid().addSelectionListener(new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent e)
			{
				// TODO Auto-generated method stub
				
			}

			@Override
			public void widgetSelected(SelectionEvent e)
			{
				e.getSource();
			}});
		
		for (int i = 0; i < cols; i++)
		{
			GridColumn column = new GridColumn(v.getGrid(), SWT.NONE);
			column.setWidth(200);
		}
		ReviewCard[][] model = new ReviewCard[rows][cols];

		int i = 0;
		for (Iterator<ReviewCard> iterator = cards.values().iterator(); iterator.hasNext();)
		{
			ReviewCard card = iterator.next();
			model[i%rows][i/rows] = card;
			i++;
		}
		v.setInput(model);
		
		
//		super.init(cards);
	}

	public List<String> getSelected()
	{
		List<String> ret = new ArrayList<String>();
		
		GridItem[] items = v.getGrid().getSelection();
		
		for (int i = 0; i < items.length; i++)
		{
			items[i].getData();
			
			ret.add(items[i].getText());
		}
		
		return ret;
	}
	public Cards getSelectedCards()
	{
		
		
		Cards ret = new Cards();
	
		
		return ret;
	}
	
	/**
	 * @param card
	 */
	@Override
	protected void addCardView(CardView cardView)
	{
		// TODO Auto-generated method stub
		
	}

	/**
	 * @param card
	 * @return
	 */
	protected CardView createCardView(ReviewCard card)
	{
		return  null;
	}
}